package com.jfinal.weixin.weixinbot;

import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.jfinal.core.Controller;

import cn.zhouyafeng.itchat4j.core.Core;

/**
 * 
 * 
 * @author fanjunjian
 * 
 */
public class WeiXinLoginController extends Controller {

	public void index() {
		
	}
	
	public void updateAutoReply() {
		String userName = getPara("UserName");
		boolean autoReply = getParaToBoolean("AutoReply");
		Core.getInstance().getAutoReply().put(userName, autoReply);
		if (Core.getInstance().getAutoReply().get(userName) == autoReply) {
			JsonResultModel jrm = new JsonResultModel("200", "设置成功!");
			renderJson(jrm);
		} else {
			JsonResultModel jrm = new JsonResultModel("300", "设置失败!");
			renderJson(jrm);
		}
		
	}
	
	public void loginUserInfo() {
		Map<String, Object> loginInfo = Core.getInstance().getLoginInfo();
		JSONObject userSelf = Core.getInstance().getUserSelf();
		String userName = Core.getInstance().getUserName();
		String nickName = Core.getInstance().getNickName();
		String uuid = Core.getInstance().getUuid();
		List<JSONObject> contactList = Core.getInstance().getContactList();
		List<JSONObject> groupList = Core.getInstance().getGroupList();
		Map<String, Boolean> autoReply = Core.getInstance().getAutoReply();
		System.out.println(groupList);
		setAttr("contactList", contactList);
		setAttr("groupList", groupList);
		setAttr("autoReply", autoReply);
		setAttr("userName", userName);
		setAttr("nickName", nickName);
		setAttr("uuid", uuid);
		setAttr("userSelf", userSelf);
		setAttr("loginInfo", loginInfo);
		render("weixinLogin/loginUserInfo.ftl");
	}
	
	public void createQR() {
		String qrPath = WeixinbotDemo.getInstance().createQR();
		setAttr("qrPath", qrPath+"/QR.jpg");
		System.out.println("qrPath======" + qrPath);
		render("weixinLogin/login.ftl");
	}
	
	public void weLogin() {
		boolean login = WeixinbotDemo.getInstance().weLoginOneTime();
		System.out.println("========>" + login);
		if (login) {
			WeixinbotDemo.getInstance().startMsg();
			JsonResultModel jrm = new JsonResultModel("200", "登入成功!");
			renderJson(jrm);
		} else {
			JsonResultModel jrm = new JsonResultModel("300", "登入失败!");
			renderJson(jrm);
		}
		
	}
	
	public void login() {
		System.out.println("============");
		String qrPath = WeixinbotDemo.weStart();
		setAttr("qrPath", qrPath);
		System.out.println("============");
		render("weixinLogin/login.ftl");
		
	}

	public void test() {
		Map<String, Object> loginInfo = Core.getInstance().getLoginInfo();
		JSONObject userSelf = Core.getInstance().getUserSelf();
		String userName = Core.getInstance().getUserName();
		String nickName = Core.getInstance().getNickName();
		String uuid = Core.getInstance().getUuid();
		setAttr("userName", userName);
		setAttr("nickName", nickName);
		setAttr("uuid", uuid);
		setAttr("loginInfo", loginInfo);
		renderJson(userSelf);
	}
	
}
